package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.Classcardservice.ClassCardFieldService;
import com.wuseguang.springcloud.Classcardservice.ClassCardService;
import com.wuseguang.springcloud.dao.jdbc.TeacherChatJdbc;
import com.wuseguang.springcloud.service.TeacherChatService;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class TeacherChatServiceImpl implements TeacherChatService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private TeacherChatJdbc teacherChatJdbc;

    @Autowired
    private ClassCardService classCardService;

    @Autowired
    private ClassCardFieldService service;


    /**
     * @Author zm
     * @Description 查询教师列表
     * @Date 2020/9/13 11:12
     */
    @Override
    public Result queryTeacherList(Integer id) {//学生id
        String sql = "SELECT id,`student_name` name,head_portrait headPortrait,'家长' roleName,0 `type`,IF((SELECT COUNT(1) FROM parent_chat WHERE student_id=student.id AND TYPE=1 AND state=0)>0,1,0) state FROM student WHERE id=? LIMIT 1 " +
                " UNION ALL " +
                " SELECT id,`name` ,head_portrait headPortrait,(SELECT GROUP_CONCAT(r.`name`)  FROM teacher_role  tr INNER JOIN `classes` c  ON(tr.pid=c.id) LEFT JOIN roles r ON (tr.role_id=r.id) " +
                " WHERE tr.teacher_id =t.id AND tr.type=3 AND c.id=(SELECT class_id FROM student WHERE id=? LIMIT 1)) roleName,1 `type`, IF((SELECT COUNT(1) FROM teacher_chat WHERE student_id=? AND TYPE=1 AND state=0 AND teacher_id=t.id )>0,1,0) state " +
                "FROM `teacher` t WHERE  EXISTS (SELECT * FROM `teacher_role` tr WHERE tr.`teacher_id` =t.id AND tr.`pid` = (SELECT class_id FROM student WHERE id=? LIMIT 1) AND tr.type=3)";

        List<Map<String, Object>> list = teacherChatJdbc.queryListMap(sql, id, id, id, id);
        return new Result(list);
    }

    /**
     * @Author zm
     * @Description 添加留言
     * @Date 2020/9/13 12:01
     */
    @Override
    public Result addTeacherLeaveMessages(Integer id, Integer type, String content, Integer tid) {//学生id type 0 学生留的言 1教师留的言  tid教师id
        String sql = "insert into `teacher_chat` (`student_id`, `teacher_id`, `type`, `state`, `content`, `opertime`) values(?,?,?,0,?,now())";
        int i = teacherChatJdbc.updateByParameter(sql, id, tid, type, content);
        sql = "SELECT  class_id  string FROM student WHERE id=?";
        String s = teacherChatJdbc.queryString(sql, id);
        sql = "SELECT  school_id  string FROM student WHERE id=?";
        String school_id = teacherChatJdbc.queryString(sql, id);
        if (type == 1) {//教师留言 通知
            //添加通知
            sql = "insert into `all_notice` (`jurisdiction_id`, `type`, `content`, `class_id`, `teacher_id`, `student_id`, `parent_id`, `school_id`, `papplet`, `tapplet`, `bancard`, `state`, `opertime`) values('8','5','有一条教师留言，请进入学生中心查看',?,?,?,'0',?,'0','0','1','0',now());";
            teacherChatJdbc.updateByParameter(sql, s, tid, id, school_id);
            service.classesAndCards(s, "noticeflag", "1");
            classCardService.classesAndCards(s);
        } else {
            sql = "insert into `all_notice` (`jurisdiction_id`, `type`, `content`, `class_id`, `teacher_id`, `student_id`, `parent_id`, `school_id`, `papplet`, `tapplet`, `bancard`, `state`, `opertime`) values('8','5','有一条新的留言，请查看',?,?,?,'0',?,'0','1','0','0',now())";
            teacherChatJdbc.updateByParameter(sql, s, tid, id, school_id);
        }
        return i > 0 ? new Result() : new Result(CodeUtil.ADD_FAILURE);
    }


    /**
     * @Author zm
     * @Description 查询教师留言
     * @Date 2020/9/14 9:50
     */
    @Override
    public Result queryTeacherLeaveMessages(Integer id, Integer type, Integer tid) {//学生id  tid教师id
        Map<String, Object> map = new HashMap<>();
        String sql = "SELECT  t.id tid,t.student_name tname,head_portrait theadPortrait  FROM student t   WHERE t.id=?  limit 1";
        String teacherSql = "SELECT  t.id sid,t.name sname,head_portrait sheadPortrait  FROM teacher  t   WHERE t.id=?  limit 1";
        String sql1 = "SELECT  c.id,c.type,c.state,c.content,DATE_FORMAT(c.opertime,'%Y-%m-%d %H:%i') opertime FROM teacher_chat c,student t,teacher r  WHERE c.teacher_id=r.id and r.id=?  and c.student_id=t.id AND t.id=? ORDER BY c.id desc limit 0,50";
        try {
            map = teacherChatJdbc.queryMap(sql, id);
            map.putAll(teacherChatJdbc.queryMap(teacherSql, tid));
            List<Map<String, Object>> maps = teacherChatJdbc.queryListMap(sql1, tid, id);
            //让list集合倒序
            Collections.reverse(maps);
            map.put("list", maps);
        } catch (Exception e) {
            logger.info("queryStudentVacationById==>,error={}", e);
        }
        sql = "update teacher_chat set state=1  where student_id=? and type=1";
        if (type == 0) {//0 班牌
            teacherChatJdbc.updateByParameter(sql, id);
        } else {
            sql = "update teacher_chat set state=1  where student_id=? and type=0";
            teacherChatJdbc.updateByParameter(sql, id);
        }
        return new Result(map);
    }

}
